package com.itc.bpm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itc.bpm.bean.BpmModel;
import com.itc.bpm.bean.BpmProcessCommunicate;
import com.itc.bpm.service.BpmProcessCommunicateService;
import com.itc.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Tag(name = "流程沟通接口")
@RestController
@Api(tags = "流程沟通接口")
@RequestMapping("/bpm/communicate")
public class BpmProcessCommunicateController {

    @Autowired
    BpmProcessCommunicateService bpmProcessCommunicateService;
    @GetMapping("/list/{processInstanceId}")
    @ApiOperation(value = "根据流程实例id 查询消息沟通记录")
    public R<List<BpmProcessCommunicate>> getList(@PathVariable("processInstanceId") String processInstanceId)
    {
       List<BpmProcessCommunicate> list = bpmProcessCommunicateService.list(new LambdaQueryWrapper<BpmProcessCommunicate>().eq(BpmProcessCommunicate::getProcessInstanceId,processInstanceId).eq(BpmProcessCommunicate::getDelFlag,0).orderByAsc(BpmProcessCommunicate::getCreateTime));
        return R.ok(list);
    }
    @ApiOperation(value = "新增消息")
    @PostMapping(value = "/add",consumes = "application/json")
    public R addBpmProcessCommunicate(@RequestBody BpmProcessCommunicate bpmModel){
        bpmModel.setDelFlag(0);
        bpmProcessCommunicateService.save(bpmModel);
        return R.ok();
    }

    @ApiOperation(value = "删除消息")
    @DeleteMapping(value = "/delete/{id}",consumes = "application/json")
    public R deleteBpmProcessCommunicate(@PathVariable("id") Long id){
        BpmProcessCommunicate bpmProcessCommunicate =bpmProcessCommunicateService.getById(id);
        if(bpmProcessCommunicate==null){
            return R.fail("消息不存在!");
        }
        bpmProcessCommunicate.setDelFlag(0);
        bpmProcessCommunicateService.updateById(bpmProcessCommunicate);
        return R.ok();
    }

}
